﻿using System.Collections.Generic;

namespace Hydrogen.Cmd.Interfaces
{
    public abstract class CommandBase : ICommand
    {
        public static string Discription { get; }
        public abstract void Invoke(params string[] args);

        protected static Dictionary<string, string> GetArgsDic(string[] args, out List<string> oldArgsGroup)
        {
            Dictionary<string, string> result = new Dictionary<string, string>();
            oldArgsGroup = new List<string>();
            oldArgsGroup.Add(args[0]);

            string pFlag = "-u";
            string tempPFlag = pFlag;
            for (int i = 1; i < args.Length; i++)
            {
                if (args[i].StartsWith("-")) tempPFlag = args[i];
                else
                {
                    if (!string.Equals(pFlag, tempPFlag)) result[tempPFlag] = args[i];
                    oldArgsGroup.Add(args[i]);
                    tempPFlag = pFlag;
                }
            }

            return result;
        }

        protected static string GetArgs(Dictionary<string, string> argsDic, string key, string defaultValue)
        {
            if (argsDic == null || 
                !argsDic.TryGetValue(key, out string result)) return defaultValue;
            return result;
        }
    }
}
